﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Web;

namespace NLBS.Install
{
    public class LbsBlogTran
    {

        private OleDbConnection _dataSouce;

        public OleDbConnection AccessConn
        {
            get { return _dataSouce; }
            set { _dataSouce = value; }
        }

        private string _sqldataSouce;

        public string SqlConnectionString
        {
            get { return _sqldataSouce; }
            set { _sqldataSouce = value; }
        }

        private string _oldedbdataSouceString;

        public string OldDbConnectionString
        {
            get { return _oldedbdataSouceString; }
            set { _oldedbdataSouceString = value; }
        }

        private GenericDatabase _accessDb;

        public GenericDatabase AccessDb
        {
            get { return _accessDb; }

        }

        public LbsBlogTran()
        {
            string path = HttpContext.Current.Server.MapPath("/App_Data/blog.mdb");
            _oldedbdataSouceString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;" +
                              "data source={0}", path);
            string sql1 = HttpContext.Current.Server.MapPath("Installation01.sql");
            string sql2 = HttpContext.Current.Server.MapPath("Installation02.sql");
            NLBS.Utility.SqlHelper.RunAccessSQL(sql1, _oldedbdataSouceString, 600);
            _accessDb = new GenericDatabase(_oldedbdataSouceString, System.Data.OleDb.OleDbFactory.Instance);
           TranTag();
            TranArticle();
            NLBS.Utility.SqlHelper.RunAccessSQL(sql2, _oldedbdataSouceString, 600);
        }

        public void TranArticle()
        {
            DbCommand dbCommand = AccessDb.GetSqlStringCommand("update blog_article set log_content1=log_content0+log_content1");
            AccessDb.ExecuteNonQuery(dbCommand);
        }

        public void TranTag()
        {
            DbCommand dbCommand = AccessDb.GetSqlStringCommand("select log_id,log_tags from blog_article");
            using (IDataReader dr = AccessDb.ExecuteReader(dbCommand))
            {
                while (dr.Read())
                {
                    int log_id = Convert.ToInt32(dr["log_id"].ToString());
                    string tags = dr["log_tags"].ToString().Trim();
                    if (!string.IsNullOrEmpty(tags))
                    {
                        string[] tagList = tags.Split(new char[] { ',' });
                        string sql = "insert into blog_tag (log_id,tag_name) values({0},'{1}')";
                        foreach (string item in tagList)
                        {
                            dbCommand = AccessDb.GetSqlStringCommand(string.Format(sql, log_id, item));
                            AccessDb.ExecuteNonQuery(dbCommand);
                        }
                    }
                }
                //dbCommand = AccessDb.GetSqlStringCommand("ALTER TABLE [blog_Article]  DROP  COLUMN log_tags;");
                //AccessDb.ExecuteNonQuery(dbCommand);

            }
        }
    }
}
